{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn import svm\n",
    "from sklearn import tree\n",
    "from sklearn.pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Load and split the data\n",
    "iris = load_iris()\n",
    "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Construct svm pipeline\n",
    "\n",
    "pipe_svm = Pipeline([('ss1', StandardScaler()),\n",
    "\t\t\t('pca', PCA(n_components=2)),\n",
    "\t\t\t('svm', svm.SVC(random_state=42))])\n",
    "\t\t\t\n",
    "# Construct knn pipeline\n",
    "pipe_knn = Pipeline([('ss2', StandardScaler()),\n",
    "\t\t\t('knn', KNeighborsClassifier(n_neighbors=6, metric='euclidean'))])\n",
    "\n",
    "# Construct DT pipeline\n",
    "pipe_dt = Pipeline([('ss3', StandardScaler()),\n",
    "\t\t\t('minmax', MinMaxScaler()),\n",
    "\t\t\t('dt', tree.DecisionTreeClassifier(random_state=42))])\n",
    "\n",
    "# Construct Random Forest pipeline\n",
    "num_trees = 100\n",
    "max_features = 1\n",
    "pipe_rf = Pipeline([('ss4', StandardScaler()),\n",
    "\t\t\t('pca', PCA(n_components=2)),\n",
    "\t\t\t('rf', RandomForestClassifier(n_estimators=num_trees, max_features=max_features))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pipe_dic = {0: 'K Nearest Neighbours', 1: 'Decision Tree', 2:'Random Forest', 3:'Support Vector Machines'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pipelines = [pipe_knn, pipe_dt,pipe_rf,pipe_svm]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for pipe in pipelines:\n",
    "\tpipe.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K Nearest Neighbours pipeline test accuracy: 1.00\n",
      "Decision Tree pipeline test accuracy: 1.00\n",
      "Random Forest pipeline test accuracy: 0.90\n",
      "Support Vector Machines pipeline test accuracy: 0.90\n"
     ]
    }
   ],
   "source": [
    "for idx, val in enumerate(pipelines):\n",
    "\tprint('%s pipeline test accuracy: %.2f' % (pipe_dic[idx], val.score(X_test, y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K Nearest Neighbours is the classifier has the best accuracy of 1.00\n"
     ]
    }
   ],
   "source": [
    "best_accuracy = 0\n",
    "best_classifier = 0\n",
    "best_pipeline = ''\n",
    "for idx, val in enumerate(pipelines):\n",
    "\tif val.score(X_test, y_test) > best_accuracy:\n",
    "\t\tbest_accuracy = val.score(X_test, y_test)\n",
    "\t\tbest_pipeline = val\n",
    "\t\tbest_classifier = idx\n",
    "print('%s is the classifier has the best accuracy of %.2f' % (pipe_dic[best_classifier],best_accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
